$(function(){
	//首页加载数据
	searchOrder();
});

//查询订单数据
function searchOrder() {
	$.ajax({
		type : "post",
		url : "/myservice/order/upgradeManage/queryList",
		async : false,
		dataType : "json",
		success : function(data){
			$('#upgradeList').empty();
			//新增内容
			var tb_content = '';
			if(data.orders.length > 0) {
				$.each(data.orders,function(index,order){
					tb_content += '<tr>'
						 	   + '<td data-id="'+order.orderId+'">'+order.orderNum+'</td>' 
							   + '<td>'+order.goodsName+'</td>'
							   + '<td>'+(parseFloat(order.payMoney).toFixed(2))+'</td>'
							   + '<td>'+order.userNum+'</td>'
							   + '<td>'+checkIsDredge(order.isDredge)+'</td>'
							   + '<td>'+checkDoState(order.orderId,order.isDredge)+'</td>'
							   + '</tr>';
				})
				
			}else {
				tb_content += '<tr><td colspan="6">没有符合条件的订单，请尝试其他搜索条件。</td></tr>';
			}
			$('#upgradeList').append(tb_content);
		}
	});
}

//判断用户数
function checkUserNum(userNum,dataExtend) {
	var str = '';
	if(dataExtend == null || dataExtend == '') {
		str = userNum;
	}else {
		var arr = dataExtend.split("&");
		str = arr[1];
	}
	return str;
}

//判断操作显示
function checkDoState(orderId,isDredge) {
	var str = '';
	if(isDredge == null || isDredge == '' || isDredge == 0){
		str = '<a href="javascript:void(0)" class="uc-button" onclick="dredge('+orderId+')">开通</a>';
	}else{
		str = '<a href="javascript:void(0)" class="uc-button" onclick="upgrade('+orderId+')">升级</a>';
	}
	return str;
}


//判断支付金额
function checkPayMoney(payMoney) {
	var str = '';
	if(orderState == 0){
		str = parseFloat(payMoney).toFixed(2);
	}else{
		str = parseFloat(orderMoney).toFixed(2);
	}
	return str;
}

//判断购买类型
function checkIsFree(isFree,buyType) {
	var str = '';
	isFree = $.trim(isFree);
	if(isFree == null || isFree==''){
		str = '&nbsp;';
	}else{
		if(isFree == 0) {
			if(buyType == 0) {
				str = '购买';
			}else if(buyType == 0) {
				str = '续费';
			}else {
				str = '升级';
			}
		}else if(isFree == 1) {
			str = '试用';
		}
	}
	return str;
}

//判断是否开通
function checkIsDredge(isDredge) {
	var str = '';
	if(isDredge == null || isDredge == '' || isDredge == 0){
		str = '未开通';
	}else{
		str = '已开通';
	}
	return str;
}

//判断付款时间
function checkPayTime(payTime) {
	var str = '';
	if(payTime==null||payTime==''){
		str = '&nbsp;';
	}else{
		str = toDate(payTime,"yyyy-MM-dd hh:mm:ss")
	}
	return str;
}

//直接查询某页
function ajax2Page(tbs,orderState) {
	searchOrder(tbs,orderState,$('#currPage'+tbs).val());
}

//向上翻页
function ajaxPageUp(tbs,orderState,page) {
	if(page == 1) {
		BSTYUN.dialog.warn("已经是第一页");
		return;
	}else {
		var num = parseInt(page) - 1;
		searchOrder(tbs,orderState,num);
	}
}

//向下翻页
function ajaxPageDown(tbs,orderState,page) {
	if(page == $("#pageCount"+tbs).val()) {
		BSTYUN.dialog.warn("已经是最后一页");
		return;
	}else {
		var num = parseInt(page) + 1;
		searchOrder(tbs,orderState,num);
	}
}

//升级
function upgrade(orderId){
	//校验是否可以升级
	$.ajax({
		type : "post",
		url : "/myservice/order/checkOrderUpgrade?orderId="+orderId,
		dataType : "json",
		success : function(data) {
			if(data.isSuccess) {
				window.open('/myservice/order/toUpgradePage?orderId='+orderId);
			}else {
				BSTYUN.dialog.warn(data.message);
			}
		}
	})
}

//开通应用
function dredge(orderId){
	$.ajax({
		type : "post",
		url : "/eos/dredgeApply?orderId="+orderId,
		dataType : "json",
		beforeSend : function() {
			var loading = function(html,callback){
		        var dialog = null;
		        dialog = $.dialog({
		            id: 'loading',
		            fixed:true,
		            title:false,
		            lock:true,
		            padding:'10px 20px',
		            content: '<div class="loading-tip">'+html+'</div>'
		        });
		    }
			loading('<span style="color:red;font-size:16px;">开通正在进行,时长约为1分钟！请耐心等待！<br>开通成功后，页面会自动刷新！</span>');
		},
		success : function(data){
			BSTYUN.dialog.close("loading");
			if(data) {
				searchOrder();
			}else {
				BSTYUN.dialog.warn("网络异常，开通失败，请重试！");
			}
		},
		error : function() {
			BSTYUN.dialog.close("loading");
			BSTYUN.dialog.warn("网络错误，请稍后重试！");
		}
	})
}